{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/jerryjliu/llama_index/blob/main/docs/examples/embeddings/bedrock.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bedrock Embeddings\n",
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-embeddings-bedrock"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from llama_index.embeddings.bedrock import BedrockEmbedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embed_model = BedrockEmbedding.from_credentials(\n",
    "    aws_access_key_id=os.getenv(\"AWS_ACCESS_KEY_ID\"),\n",
    "    aws_secret_access_key=os.getenv(\"AWS_SECRET_ACCESS_KEY\"),\n",
    "    aws_session_token=os.getenv(\"AWS_SESSION_TOKEN\"),\n",
    "    aws_region=\"<aws-region>\",\n",
    "    aws_profile=\"<aws-profile>\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embedding = embed_model.get_text_embedding(\"hello world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List supported models\n",
    "\n",
    "To check list of supported models of Amazon Bedrock on LlamaIndex, call `BedrockEmbedding.list_supported_models()` as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.embeddings.bedrock import BedrockEmbedding\n",
    "import json\n",
    "\n",
    "supported_models = BedrockEmbedding.list_supported_models()\n",
    "print(json.dumps(supported_models, indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Provider: Amazon\n",
    "Amazon Bedrock Titan embeddings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.embeddings.bedrock import BedrockEmbedding\n",
    "\n",
    "model = BedrockEmbedding().from_credentials(\n",
    "    model_name=\"amazon.titan-embed-g1-text-02\"\n",
    ")\n",
    "embeddings = model.get_text_embedding(\"hello world\")\n",
    "print(embeddings)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Provider: Cohere\n",
    "\n",
    "### cohere.embed-english-v3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = BedrockEmbedding().from_credentials(\n",
    "    model_name=\"cohere.embed-english-v3\"\n",
    ")\n",
    "coherePayload = {\n",
    "    \"texts\": [\"This is a test document\", \"This is another test document\"],\n",
    "    \"input_type\": \"search_document\",\n",
    "    \"truncate\": \"NONE\",\n",
    "}\n",
    "embeddings = model.get_text_embedding(coherePayload)\n",
    "print(embeddings)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MultiLingual Embeddings from Cohere "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = BedrockEmbedding().from_credentials(\n",
    "    model_name=\"cohere.embed-multilingual-v3\"\n",
    ")\n",
    "coherePayload = {\n",
    "    \"texts\": [\n",
    "        \"This is a test document\",\n",
    "        \"తెలుగు అనేది ద్రావిడ భాషల కుటుంబానికి చెందిన భాష.\",\n",
    "    ],\n",
    "    \"input_type\": \"search_document\",\n",
    "    \"truncate\": \"NONE\",\n",
    "}\n",
    "embeddings = model.get_text_embedding(coherePayload)\n",
    "print(embeddings)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
